package com.paomi.client.activity.detail.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.fragment.order.model.OrderListBean;
import com.paomi.client.utils.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderDetailAdapter extends RecyclerView.Adapter<OrderDetailAdapter.ViewHolder> {
    private Context context;
    private List<OrderListBean.OrderGoods> data;
    private OrderListBean.OrderData orderData;

    public OrderDetailAdapter(Context context, OrderListBean.OrderData orderData) {
        this.context = context;
        this.orderData = orderData;

    }

    public void setData(List<OrderListBean.OrderGoods> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_detail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(orderData!=null){
            Util.setWidthAndHeight(holder.orderIv, -1, (int) (Util.getDisplay((Activity) context).widthPixels * 0.4));
            GlideApp.with(context).load(orderData.getGoodsImg()).placeholder(R.mipmap.img_default_goods).into(holder.orderIv);
            holder.orderTicketName.setText(orderData.getGoodsName());
            if(orderData.getGoodsShowName()!=null) {
                String[] ss = orderData.getGoodsShowName().split("-");
                if(ss.length>2) {
                    holder.orderTicketTime.setText(ss[1] + "月" + ss[2] + "日");
                }
            }
            holder.orderTicketSite.setText(orderData.getVenueName());
            holder.orderTicketPrice.setText("¥"+data.get(position).getAmount());
            holder.orderTicketNum.setText("X"+data.get(position).getNum());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.order_iv)
        RoundedImageView orderIv;
        @BindView(R.id.order_ticket_name)
        TextView orderTicketName;
        @BindView(R.id.order_ticket_time)
        TextView orderTicketTime;
        @BindView(R.id.order_ticket_site)
        TextView orderTicketSite;
        @BindView(R.id.order_ticket_price)
        TextView orderTicketPrice;
        @BindView(R.id.order_ticket_num)
        TextView orderTicketNum;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
